package com.ys.servers.versions.service.impl;

import com.ys.servers.common.util.CommonUtils;
import com.ys.servers.versions.dao.impl.AppVersionsDataDaoImpl;
import com.ys.servers.versions.data.AppType;
import com.ys.servers.versions.data.AppVersionsData;
import com.ys.servers.versions.dao.AppVersionsDataDao;
import com.ys.servers.versions.data.AppVersionsState;
import com.ys.servers.versions.service.AppVersionsService;
import io.vertx.core.Future;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;

/**
 * Created by taotao on 2017/5/12.
 */
public class AppVersionsServiceImpl implements AppVersionsService {

    private AppVersionsDataDao appVersionsDataDao;

    public AppVersionsDataDao getAppVersionsDataDao() {
        return appVersionsDataDao;
    }

    public AppVersionsServiceImpl setAppVersionsDataDao(AppVersionsDataDao appVersionsDataDao) {
        this.appVersionsDataDao = appVersionsDataDao;
        return this;
    }

    public AppVersionsServiceImpl() {
        setAppVersionsDataDao(new AppVersionsDataDaoImpl());
    }

    @Override
    public AppVersionsService addAppVersionsData(AppVersionsData appVersionsData, Handler<AsyncResult<AppVersionsData>> resultHandler) {
        appVersionsDataDao.saveAppVersionsData(appVersionsData).setHandler(resultHandler);
        return this;
    }

    @Override
    public AppVersionsService getAppVersionsData(String versions, AppType appType, AppVersionsState appVersionsState,
                                                 Handler<AsyncResult<AppVersionsData>> resultHandler) {

        appVersionsDataDao.getAppVersionsData(versions, appType, appVersionsState).setHandler(res -> {
            if (CommonUtils.validAsyncResult(res)) {
                resultHandler.handle(res);
            } else {
                String[] vDigits = versions.split("\\.");
                if (vDigits.length > 3) {
                    String tryVersionBig = vDigits[0] + "." + vDigits[1] + "." + vDigits[2]+".0";
                    appVersionsDataDao.getAppVersionsData(tryVersionBig, appType, appVersionsState).setHandler(bigRes -> {
                        if (CommonUtils.validAsyncResult(bigRes)) {
                            resultHandler.handle(bigRes);
                        } else {
                            haveNoVersionData(versions, appType, appVersionsState, resultHandler);
                        }
                    });
                } else
                    haveNoVersionData(versions, appType, appVersionsState, resultHandler);
            }
        });
        //.setHandler(resultHandler);
        return this;
    }

    private void haveNoVersionData(String versions, AppType appType, AppVersionsState appVersionsState,
                                   Handler<AsyncResult<AppVersionsData>> resultHandler) {
        AppVersionsData appVersionsData = new AppVersionsData().setVersions(versions)
                .setAppType(appType).setAppVersionState(appVersionsState).setRemindUrl("")
                .setUrl("").setVisitsUrl("").setDeploymentKey("");
        appVersionsData.setId(versions + ":" + appType + ":" + appVersionsState);
        Future<AppVersionsData> future = Future.future();
        future.complete(appVersionsData);
        future.setHandler(resultHandler);
    }
}
